﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Caching;
using System.Web.Security;

namespace CoHSIN
{
    public static class Globals
    {
        static Globals()
        {
        }

        public static CoHSIN.Models.CoHSINDatabaseDataContext cohsinDB
        {
            get
            {
                if (HttpContext.Current.Cache["cohsinDB"] == null)
                {
                    HttpContext.Current.Cache["cohsinDB"] = new CoHSIN.Models.CoHSINDatabaseDataContext();
                }

                return (CoHSIN.Models.CoHSINDatabaseDataContext)HttpContext.Current.Cache["cohsinDB"];

            }
            set { }
        }

        public static void setCohsinDB()
        {
            HttpContext.Current.Cache["cohsinDB"] = new CoHSIN.Models.CoHSINDatabaseDataContext();
            cohsinDB = (CoHSIN.Models.CoHSINDatabaseDataContext)HttpContext.Current.Cache["cohsinDB"];
        }

        public static string UserName
        {
            get
            {
                if (HttpContext.Current.Cache["UserName"] == null)
                {
                    HttpContext.Current.Cache["UserName"] = System.Environment.UserName;
                }

                return HttpContext.Current.Cache["UserName"].ToString();
            }
        }

        public static bool isAdmin
        {
            get
            {
                if (HttpContext.Current.Cache["isAdmin"] == null)
                {
                    HttpContext.Current.Cache["isAdmin"] = Roles.IsUserInRole("Admins");
                }

                return bool.Parse(HttpContext.Current.Cache["isAdmin"].ToString());
            }
        }
    
        public static string getDefaultPageName()
        {     
            string defaultPage = (from page in cohsinDB.Pages
                                  orderby page.PageOrder
                                  select page.PageName).FirstOrDefault();

            return defaultPage;
        }

        public static Models.Page getPage(string pageName)
        {
            int pageID = getPageID(pageName);

            Models.Page Page = (from page in cohsinDB.Pages
                                where page.PageID==pageID
                                select page).FirstOrDefault();
            return Page;
        }

        public static Models.Page getPage(int PageID)
        {

            Models.Page Page = (from page in cohsinDB.Pages
                                where page.PageID == PageID
                                select page).FirstOrDefault();

            return Page;
        }

        public static Models.Tab getTab(Models.Page Page, string tabName)
        {
            tabName = tabName.ToLower();

            Models.Tab Tab = (from tab in cohsinDB.Tabs
                              where tab.TabName.ToLower() == tabName && tab.PageID == Page.PageID
                              select tab).FirstOrDefault();

            return Tab;
        }

        public static Models.Tab getTab(int tabID)
        {

            var Tab = (from tab in cohsinDB.Tabs
                              where tab.TabID==tabID
                              select tab).FirstOrDefault();

            return Tab;
        }
        

        public static string getDefaultTabName(string Page)
        {
            if (string.IsNullOrEmpty(Page)) { Page = getDefaultPageName(); }
            Page = Page.ToLower();

            int pageID = (from page in cohsinDB.Pages
                          where page.PageName.ToLower()==Page
                          select page.PageOrder).FirstOrDefault();

            int tabID = (from tab in cohsinDB.Tabs
                         where tab.PageID==pageID
                         orderby tab.TabOrder
                         select tab.TabID).FirstOrDefault();

            string defaultTab = (from tab in cohsinDB.Tabs
                                 where tab.TabID == tabID
                                 select tab.TabName).FirstOrDefault();

            return defaultTab;
        }

        public static bool pageExists(string pageName)
        {
            pageName=pageName.ToLower();

            string sPage = (from page in cohsinDB.Pages
                           where page.PageName.ToLower() == pageName
                           select page.PageName).FirstOrDefault();

            return !string.IsNullOrEmpty(sPage);
        }

        public static bool tabExists(int pageID, string tabName)
        {
            tabName=tabName.ToLower();

            string sTab = (from tab in cohsinDB.Tabs
                           where tab.PageID == pageID && tab.TabName == tabName
                           select tab.TabName).FirstOrDefault();

            return !string.IsNullOrEmpty(sTab);
        }

        public static bool tabExists(string pageName, string tabName)
        {
            int pageID = getPageID(pageName);

            return tabExists(pageID, tabName);
        }

        public static int getPageID(string pageName)
        {
            pageName = pageName.ToLower();

            int? iPageID = (from page in cohsinDB.Pages
                           where page.PageName.ToLower() == pageName
                           select page.PageID).FirstOrDefault();

            return (iPageID < 1 ? -1 : (int)iPageID);
        }

        public static List<Models.Tab> getAllTabsForPage (string pageName)
        {
            pageName = pageName.ToLower();
            int pageID = getPageID(pageName);

            List<Models.Tab> Tabs = (from tab in cohsinDB.Tabs
                                     where tab.PageID==pageID
                                     select tab).ToList();

            return Tabs;
        }

        public static List<Models.Tab> getAllTabsForPage(int pageID)
        {

            List<Models.Tab> Tabs = (from tab in cohsinDB.Tabs
                                     where tab.PageID == pageID
                                     select tab).ToList();

            return Tabs;
        }

    }
}
